#ifndef LABELMEX_LABEL_DIALOG_H
#define LABELMEX_LABEL_DIALOG_H

#include <QCompleter>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QPushButton>
#include <QRegularExpressionValidator>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QWidget>
#include <QCursor>
#include <QCheckBox>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QDebug>
#include <QKeyEvent>

class LabelQLineEdit : public QLineEdit
{
	Q_OBJECT

public:
	void setListWidget(QListWidget* listWidget);

protected:
	void keyPressEvent(QKeyEvent* e) override;

private:
	QListWidget* listWidget;
};


struct LabelDialogPopUpResult {
    QString label;
    QMap<QString, bool> flags;
    int groupId;
    QString description;
};

class LabelDialog : public QDialog
{
	Q_OBJECT

public:
	LabelDialog(
		const QString& text = "Enter object label",
		QWidget* parent = nullptr,
		const QStringList& labels = QStringList(),
		bool sortLabels = true,
		bool showTextField = true,
		const QString& completion = "startswith",
		const QMap<QString, QStringList>& flags = {},
		QMap<QString, bool> fitToContent = {{"row", false}, {"column", true}}
	);

	QString getLabel() const;

	int getGroupId() const;
	QString getDescription() const;

	QMap<QString, bool> getFlags() const;

	void resetFlags(const QString& label = "");

	void setFlags(const QMap<QString, bool>& flags);

	void validate();

	LabelDialogPopUpResult popUp(
		const QString& text = QString(),
		bool move = true,
		const QMap<QString, bool>& flags = {},
		int groupId = -1,
		const QString& description = QString()
	);

	QLineEdit* edit;
	QLineEdit* editGroupId;
	QTextEdit* editDescription;
	QListWidget* labelList;
	QDialogButtonBox* buttonBox;
	QVBoxLayout* flagsLayout;

private slots:
	void labelSelected(QListWidgetItem* item);

	void labelDoubleClicked(QListWidgetItem* /*item*/);

	void updateFlags(const QString& label);

private:
	QMap<QString, bool> _fitToContent;
	bool _sortLabels;
	QMap<QString, QStringList> _flags;
};

#endif //LABELMEX_LABEL_DIALOG_H
